<template>
    <div style="width: 100%;">
        <div style="width: 90%;margin: 0 auto">
            <div style="width: 100%;">
                <span style="font-weight: 700;font-size: 20px">|基本信息</span>
            </div>
            <el-divider></el-divider>
            <el-form style="width: 100%;" label-width="100px">
                <el-container>
                    <el-form-item label="计划编号：">
                        <el-input v-model="subTenderPlan.id" readonly style="width: 500px;"></el-input>
                    </el-form-item>
                    <el-form-item label="日期：" style="margin-left: 10px">
                        <el-input v-model="subTenderPlan.date" readonly style="width: 500px;"></el-input>
                    </el-form-item>
                </el-container>
                <el-container>
                    <el-form-item label="项目选择：">
                        <el-input v-model="subTenderPlan.projectName" readonly style="width: 500px;"
                                  @focus="projectDialogVisible=true" disabled></el-input>
                        <el-input v-model="subTenderPlan.proId" v-show="false"></el-input>
                    </el-form-item>
                    <el-form-item label="编制人：" style="margin-left: 10px">
                        <el-input v-model="subTenderPlan.planEditor" readonly style="width: 500px;" disabled></el-input>
                    </el-form-item>
                </el-container>
                <el-container>
                    <el-form-item label="备注：">
                        <el-input v-model="subTenderPlan.comment" type="textarea"
                                  disabled
                                  style="width: 1110px;"></el-input>
                    </el-form-item>
                </el-container>
            </el-form>
        </div>

        <multi-file-upload
                style="width: 90%;margin: 20px auto"
                title="附件"
                action="http://localhost/uploads"
                :params="{folder: 'subTenderPlan'}"
                :headers="{token: token}"
                :show-button="false"
                :parent-file-list="fileList"
        ></multi-file-upload>

        <div style="width: 90%;margin: 30px auto">
            <div style="width: 100%;">
                <div style="font-weight: 700;font-size: 20px;display: flex">
                    <div>|分包招标采购明细</div>
                </div>
            </div>
            <el-divider></el-divider>
            <el-table :data="subTenderDetails">
                <el-table-column prop="index" v-if="false"></el-table-column>
                <el-table-column label="分包项目" prop="subProject">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.subProject" disabled></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="分包工作内容" prop="subContent">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.subContent" disabled></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="组织招标方" prop="tenderer">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.tenderer" readonly></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="分包方式" prop="subTenderType">
                    <template slot-scope="scope">
                        <el-select disabled v-model="scope.row.subTenderType" :value="scope.row.subTenderType">
                            <el-option label="包工包料" value="1"></el-option>
                            <el-option label="劳务" value="2"></el-option>
                            <el-option label="包工及部分材料" value="3"></el-option>
                            <el-option label="其他" value="4"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="分包商选择方式" prop="subProviderChoose">
                    <template slot-scope="scope">
                        <el-select disabled v-model="scope.row.subProviderChoose" :value="scope.row.subProviderChoose">
                            <el-option label="公司选定" value="1"></el-option>
                            <el-option label="业主选定" value="2"></el-option>
                            <el-option label="项目选定" value="3"></el-option>
                            <el-option label="项目选择公司批准" value="4"></el-option>
                            <el-option label="业主指定我方签合同" value="5"></el-option>
                            <el-option label="业主项目共同选择" value="6"></el-option>
                            <el-option label="其他" value="7"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-divider></el-divider>

        <div style="width: 90%;margin: 30px auto">
            <div style="width: 100%;">
                <div style="font-weight: 700;font-size: 20px;display: flex">
                    <div>|竞标信息</div>
                </div>
            </div>
            <el-divider></el-divider>
            <el-form label-width="100px">
                <el-form-item label="报价：">
                    <el-input-number :disabled="checkPlan" v-model="subBiddingHistory.offer" style="width: 500px;"></el-input-number>
                </el-form-item>
                <el-form-item label="竞标文件：" v-if="!checkPlan">
                    <el-upload
                            style="width: 500px;"
                            class="upload-demo"
                            ref="upload"
                            name="multipartfile"
                            action="http://localhost/upload"
                            :headers="{token:this.$store.state.token}"
                            :data="{folder:'subBidding'}"
                            :show-file-list="true"
                            :on-success="handleUploadSuccess"
                            :limit="1"
                            :auto-upload="false">
                        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input
                            :disabled="checkPlan"
                            v-model="subBiddingHistory.remark"
                            type="textarea"
                            placeholder="描述"
                            style="width: 1100px;"
                            :autosize="{ minRows: 5}"
                    ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button v-if="!checkPlan" type="primary" @click="handleSubmit">竞标</el-button>
                    <el-button @click="handleReturn">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import {DateUtil} from "../../../model/common/DateUtil";
    import {SubTenderPlanService} from "../../../model/gs/SubTenderPlanService";
    import {SubTenderDetailService} from "../../../model/gs/SubTenderDetailService";
    import {AttachemenInfoService} from "../../../model/gs/AttachemenInfoService";
    import {SubBiddingHistoryService} from "../../../model/gs/SubBiddingHistoryService";

    const subTenderPlanService = SubTenderPlanService.getInstance()
    const subTenderDetailService = SubTenderDetailService.getInstance()
    const attachemenInfoService = AttachemenInfoService.getInstance()
    const subBiddingHistoryService = SubBiddingHistoryService.getInstance()

    export default {
        props:['plan'],
        data() {
            return {
                subTenderPlan: {
                    id: '',
                    date: DateUtil.today(),
                    planEditor: sessionStorage.getItem('realName')
                },
                fileList: [],
                subTenderDetails: [],
                subBiddingHistory:{
                    subId:''
                }
            }
        },
        methods: {
            getPlanById(id) {
                subTenderPlanService.getById(id).then(response => {
                    if (response.data.code === 0) {
                        this.subTenderPlan = response.data.data
                    }
                })
            },
            getDetailList(id) {
                subTenderDetailService.getByPlan(id).then(response => {
                    if (response.data.code === 0) {
                        this.subTenderDetails = response.data.data
                    }
                })
            },
            getFileList(id) {
                attachemenInfoService.getById(id).then(response => {
                    if (response.data.code === 0) {
                        response.data.data.forEach(item => {
                            this.fileList.push({
                                fileName: item.fileName,
                                uploadPerson: this.subTenderPlan.planEditor,
                                uploadDate: item.uploadDate,
                                filePath: item.filePath
                            })
                        })
                    }
                })
            },
            handleUploadSuccess(response){
                this.subBiddingHistory.file = response.data
            },
            submitUpload(){
                this.$refs.upload.submit()
            },
            handleSubmit() {
                if(!subBiddingHistoryService.check(this.subBiddingHistory)){
                    return
                }
                if(this.subBiddingHistory.id != null){
                    subBiddingHistoryService.update(this.subBiddingHistory).then(response => {
                        if (response.data.code === 0) {
                            this.$message.success({
                                message: '竞标修改成功',
                                duration: 1000
                            })
                            this.$emit('back')
                        }else{
                            this.$message.error({
                                message:response.data.message,
                                duration:1000
                            })
                        }
                    })
                } else{
                    subBiddingHistoryService.save(this.subBiddingHistory).then(response => {
                        if (response.data.code === 0) {
                            this.$message.success({
                                message: '竞标提交成功',
                                duration: 1000
                            })
                            this.$emit('back')
                        }else{
                            this.$message.error({
                                message:response.data.message,
                                duration:1000
                            })
                        }
                    })
                }
            },
            isExist(){
                subBiddingHistoryService.exist(this.plan.id).then(response => {
                    if(response.data.code === 0){
                        if(response.data.data!=null){
                            this.subBiddingHistory = response.data.data
                        }
                    }
                })
            },
            handleReturn(){
                this.$emit('back')
            }
        },
        computed: {
            token: {
                get() {
                    return this.$store.state.token
                }
            },
            checkPlan: {
                get(){
                    return this.plan.type != null && this.plan.type != undefined
                }
            }
        },
        created() {
            this.subBiddingHistory.subId = this.plan.id
            this.getPlanById(this.plan.id)
            this.getDetailList(this.plan.id)
            this.getFileList(this.plan.attachemenIds)
            this.isExist()
        }
    }
</script>

<style scoped>

</style>
